//
//  JDDTabBarViewController.m
//  JDD
//
//  Created by 张露杰 on 17/10/23.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "HBHTabBarViewController.h"
#import "HWDNavigationController.h"
#import "HBHLoginViewController.h"

@interface HBHTabBarViewController ()<UINavigationControllerDelegate>

@property (nonatomic, strong) NSArray *nameArray;

@property (nonatomic, strong) NSArray *imageName;

@property (nonatomic, strong) NSArray *titleArr;

@end

@implementation HBHTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self  setView];
    self.delegate = self;
}

- (void)setView
{
    NSMutableArray *viewArray = [NSMutableArray array];
    for (int i = 0; i < self.nameArray.count;++i )
    {
        NSString *className = [NSString stringWithFormat:@"HBH%@ViewController",self.nameArray[i]] ;
        Class myClass = NSClassFromString(className);
        UIViewController *vc = [[myClass alloc]init];
        
        HWDNavigationController *navigation = [[HWDNavigationController alloc]initWithRootViewController:vc];
        navigation.delegate = self;
        
        vc.tabBarItem = [[UITabBarItem alloc]initWithTitle:self.titleArr[i] image:[[UIImage imageNamed:[NSString stringWithFormat:@"%@_unselect",self.imageName[i]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:[NSString stringWithFormat:@"%@_select",self.imageName[i]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        
        [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#757575"]} forState:UIControlStateNormal];
        [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:mainColor} forState:UIControlStateSelected];
        [viewArray addObject:navigation];
    }
    self.viewControllers = viewArray;
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, self.tabBar.frame.size.height)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.tabBar insertSubview:backView atIndex:0];
    self.tabBar.opaque = YES;
    
    self.tabBar.translucent = YES;
    //    self.tabBar.barStyle = 2;
}

- (NSArray *)nameArray
{
    if (!_nameArray)
    {
        _nameArray = @[@"Main",@"Find",@"Order",@"Forum",@"My"];
    }
    return _nameArray;
}

- (NSArray *)imageName
{
    if (!_imageName) {
        _imageName = @[@"Main",@"Find",@"Order",@"Forum",@"My"];
    }
    return _imageName;
}

- (NSArray *)titleArr
{
    if (!_titleArr)
    {
        _titleArr = @[@"首页",@"发现",@"订单",@"论坛",@"我的"];
    }
    return _titleArr;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if ([viewController.tabBarItem.title isEqualToString:@"论坛"] || [viewController.tabBarItem.title isEqualToString:@"我的"] || [viewController.tabBarItem.title isEqualToString:@"订单"]) {
        if (![Storage isLogin]) {
            [Storage goToLogin];
            return NO;
        }
    }
    
    return YES;
}

@end
